﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.Common.Production;
using VNet.VisiSuite.Common.Helpers;

namespace VNet.VisiSuite.Controls
{

     /// <summary>
    /// Interaction logic for ProductionControl.xaml
    /// </summary>
    public partial class ProductionControl : UserControl
    {

        #region Member variables

        #endregion

        public ProductionControl()
        {

            InitializeComponent();
        }

        #region Dependency Properties


        #endregion

        private void downtimeClick(object sender, MouseButtonEventArgs e)
        {
            var view_model = DataContext as ProductionInterface;
            if (view_model != null)
                view_model.showDowntimeDialog(false, false);
        }

        private void downtimeTouch(object sender, TouchEventArgs e)
        {
            var view_model = DataContext as ProductionInterface;
            if (view_model != null)
                view_model.showDowntimeDialog(false, false);
        }

        private void _pie_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var view_model = DataContext as ProductionInterface;
            bool start = (bool)e.NewValue;
   
            // Start if is visible
            if (start && view_model != null)
            {
                var percentages = new List<int>();
                var foregrounds = new List<Brush>();
                var backgrounds = new List<Brush>();

                if (view_model.getAlarmSettings(percentages, foregrounds, backgrounds))
                {
                    _pie.beginProgress(view_model.TargetLoadUnloadSecs, percentages, backgrounds, foregrounds);
                }
            }
            else if (!start)
            {
                _pie.stop();
            }
        }

        private void _countDownTimer_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            // Start the timer if it is visible
            if ((bool)e.NewValue)
            {
                _countDownTimer.StartTimer();
            }
            else
            {
                _countDownTimer.StopTimer();
            }
        }

        private void onLoaded(object sender, RoutedEventArgs e)
        {
            var viewmodel = this.DataContext as ProductionInterface;
            _pie.ProgressUpdate += viewmodel.handleCircularProgressUpdates;

            _pie.FontBrush = new SolidColorBrush(Color.FromRgb(58, 8, 120));
            _pie.HoleBrush = Brushes.White;
        }
    }
}
